package com.thfw.test.view.mq;

import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Handler;
import android.os.Message;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.thfw.test.MyApplication;
import com.thfw.test.utils.ToastUtil;

import org.eclipse.paho.android.service.MqttAndroidClient;

import scooper.cn.sc_base.SCLog;


/**
 * Created by scooper_lxx on 2016/12/12.
 */

@TargetApi(Build.VERSION_CODES.CUPCAKE)
public class PahoHepler {

    public static final String MSG_EVENT_PUSH = "MSG_EVENT_PUSH";
    public static final String MSG_TASK_END = "MSG_TASK_END";
    public static final String TASK_ID = "taskId";
    public static final String LIGHT_OPRATE_ACTION ="LIGHT_OPRATE_ACTION";
    public static final String LIGHT_OPRATE_ACTION_EXTRA_DATA ="LIGHT_OPRATE_ACTION";
    //加入警情列表失败
    private final static String TAG = PahoHepler.class.getCanonicalName();
    private boolean isHaveNewChange;
    private static MqttAndroidClient mAndroidClient = null;
    private final static int JX = 1;
    private static PahoHepler _instance;

    private MyApplication sipDemo;

    private MqttClientManager mMqttClientManager;


    public static PahoHepler getInstance() {
        if (_instance == null) {
            synchronized (PahoHepler.class) {
                if (_instance == null) {
                    _instance = new PahoHepler();
                }
            }
        }
        return _instance;
    }

    private PahoHepler() {
    }

    public void initConfig(MyApplication sipdemo, String mqIp, String mqPort, String username) {
        this.sipDemo = sipdemo;
        ConfigBean configBean = new ConfigBean(sipDemo.getApplicationContext());
        String serverString = String.format("tcp://%s:%s",
                mqIp,
                mqPort);
        //String server = "tcp://192.168.101.65:61613";

        String client = username;
        configBean.setApolloUrl(serverString);
        configBean.setApolloId(client);
        configBean.setApolloUserPassword("test");
        configBean.setApolloUserName("test");
        configBean.setQos(1);
        configBean.setTopic(new String[]{

                "/v1/local/opreateLamp"
                //接收到的主题
        });
        if (mMqttClientManager != null) {
            mMqttClientManager.destroy();
        }
        mMqttClientManager = new MqttClientManager(handler);
        mMqttClientManager.setConfigBean(sipDemo.getApplicationContext(), configBean);


    }

    public boolean isConnect() {
        return mMqttClientManager.isConnect();
    }

    private Handler handler = new Handler(new Handler.Callback() {

        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();

        @Override
        public boolean handleMessage(Message msg) {
            // msg.obj 为主题
            // msg.getData().getString("data") 为消息体字符串
            if (msg.what == MqttClientManager.MSG_WHAT_TOPIC) {
                String topic = (String) msg.obj;
                if ("/v1/local/opreateLamp".equals(topic)) {
                    String data = msg.getData().getString("data");

                    try {
                        ToastUtil.showToast(sipDemo,"收到开关灯指令");
                        MessageModel mqttBean = MessageModel.changeStringToModel(data);
                        Intent intent = new Intent(LIGHT_OPRATE_ACTION);
                        intent.putExtra(LIGHT_OPRATE_ACTION_EXTRA_DATA, mqttBean);
                        sipDemo.sendBroadcast(intent);
                    } catch (Exception e) {
                        SCLog.w(TAG, e.getMessage());

                    }
                }
            } else if (msg.what == MqttClientManager.MSG_WHAT_CONNECTED) {
                //已经成功订阅主题

            }
            return false;
        }
    });





    public void connect() {

        new ConnectThread().start();

    }




    private class ConnectThread extends Thread {
        @Override
        public synchronized void run() {
            if (!mMqttClientManager.isConnect()) {
                mMqttClientManager.connect();
            }
        }
    }

    public void disconnect() {
        if (mMqttClientManager != null && mMqttClientManager.isConnect()) {
            mMqttClientManager.disconnect();
        }
    }


    public void pushLightOprate(String type,int num ,String state) {
        if (mMqttClientManager != null) {
            mMqttClientManager.pushLightOprate(type,num,state);
        }
    }


}